package com.dmz.proxy.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author lee
 */
public class GMUtil {

    public static String eb(String key, String iv, String text) {
        byte[] bytes = null;
        try {
            bytes = SM4CBCUtil.encrypt(SM4CBCUtil.loadKeyBase64(key), SM4CBCUtil.loadIVBase64(iv), text.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static String db(String key, String iv, String text) {
        byte[] encryptedData = Base64.getDecoder().decode(text);
        byte[] bytes = null;
        try {
            bytes = SM4CBCUtil.decrypt(SM4Util.loadKeyBase64(key), SM4CBCUtil.loadIVBase64(iv), encryptedData);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new String(bytes, StandardCharsets.UTF_8);
    }
}
